package com.weichat.controller;

import java.util.HashMap;
import java.util.Map;
import java.util.SortedMap;
import java.util.TreeMap;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.model.Event_user;
import com.model.Propertr_user;
import com.model.User;
import com.model.locationeat;
import com.service.Event_userService;
import com.service.LocationeatService;
import com.service.Propertr_userService;
import com.service.UserService;
import com.weichat.tool.AccessTokentool;
import com.weichat.tool.Sha1Util;
import com.weichat.tool.WeiChatConfig;



@RestController
public class Web1wsqController {
	@Autowired
	AccessTokentool accessTokentool;
	@Autowired
	UserService userService;
	@Autowired
	LocationeatService locationeatService;
	@Autowired
	Propertr_userService propertr_userService;
	@Autowired
	Event_userService event_userService;
	@RequestMapping("/configer1")
	

	public Map<String,String> getJSDKconfig(@RequestParam("url") String url) throws Exception{
		Map<String,String>  configParam=new HashMap<String,String>();
		SortedMap<String, String> finalpackage = new TreeMap<String, String>();
		String timestamp = Sha1Util.getTimeStamp();
		String nonce=Sha1Util.getNonceStr();
		
		String accesstoken=(String)accessTokentool.getNewAccessToken().get("rs");
		System.out.println("====="+accesstoken);
		String Ticket=Sha1Util.getjsApiTicket("https://api.weixin.qq.com/cgi-bin/ticket/getticket?access_token=",accesstoken);
		
		finalpackage.put("timestamp", timestamp);
		finalpackage.put("noncestr", nonce);
		finalpackage.put("jsapi_ticket", Ticket);
		//finalpackage.put("url", "http://317418e1.ngrok.io/weichat/wsq/weichat-wsq.html/");
		finalpackage.put("url", url);
		
		
		
		
		String signature =Sha1Util.createSHA1Sign(finalpackage);
		configParam.put("appid", WeiChatConfig.appid);
		configParam.put("timestamp", timestamp);
		configParam.put("nonceStr", nonce);
		configParam.put("signature", signature);
		return configParam;
		
   }
	
	@RequestMapping("/wsqeatbreakfast")
	public String breakFast(HttpServletRequest req){
		String result="N";
		double latitude=Double.parseDouble(req.getParameter("latitude"));
		double longitude=Double.parseDouble(req.getParameter("longitude"));
		System.out.println("=======B"+latitude);   //纬度
		System.out.println("=======D"+longitude); //经度
		// BigDecimal longi = new BigDecimal(Double.toString(longitude));
		// BigDecimal latit= new BigDecimal(Double.toString(latitude));
//一食堂1
		locationeat l1=locationeatService.selectByU_id(1);
//一食堂2	
		locationeat l2=locationeatService.selectByU_id(2);
//二食堂1
		locationeat l3=locationeatService.selectByU_id(3);		
//二食堂2
		locationeat l4=locationeatService.selectByU_id(4);
		//u_id
	//	 int u_id=129;		
		String openid= (String) req.getSession().getAttribute("openid");
		User u=(User) req.getSession().getAttribute("user");
		int u_id=u.getId();
		
		if(l1.getLongitude()>=longitude&&longitude<=l2.getLongitude()&&l1.getLatitude()<=latitude&&latitude<=l2.getLatitude()){
			Event_user eu=new Event_user();
			eu.setUserId(u.getId());
			eu.setEventId(2);
			eu.setType("Y");	
			event_userService.insertSelective(eu);
			//eu_id
			int eu_id=event_userService.selectEidByUidOrder(u_id).getEuId();
			Propertr_user p1=new Propertr_user();
			p1.setpId(1);
			p1.setCount(5); 
			p1.setuId(u_id);
			p1.setEuId(eu_id);
			p1.setEventName("吃早餐");
			Propertr_user p2=new Propertr_user();
			p2.setpId(2);
			p2.setCount(100);
			p2.setuId(u_id);
			p2.setEuId(eu_id);
			p2.setEventName("吃早餐");
			Propertr_user p3=new Propertr_user();
			p3.setpId(3);
			p3.setCount(100);
			p3.setuId(u_id);
			p3.setEuId(eu_id);
			p3.setEventName("吃早餐");
			propertr_userService.insertSelective(p1);
			propertr_userService.insertSelective(p2);
			propertr_userService.insertSelective(p3);
			result="Y";
		}else if(l4.getLongitude()>=longitude&&longitude<=l3.getLongitude()&&l4.getLatitude()<=latitude&&latitude<=l3.getLatitude()){
			Propertr_user p1=new Propertr_user();
			p1.setpId(1);
			p1.setCount(5);
			p1.setuId(u_id);
			Propertr_user p2=new Propertr_user();
			p2.setpId(2);
			p2.setCount(100);
			p2.setuId(u_id);
			Propertr_user p3=new Propertr_user();
			p3.setpId(3);
			p3.setCount(100);
			p3.setuId(u_id);
			propertr_userService.insertSelective(p1);
			propertr_userService.insertSelective(p2);
			propertr_userService.insertSelective(p3);
			
			result="S";
			
		}else{
//			Propertr_user p1=new Propertr_user();
//			p1.setpId(1);
//			p1.setCount(-5);
//			p1.setuId(u_id);
//			Propertr_user p2=new Propertr_user();
//			p2.setpId(2);
//			p2.setCount(0);
//			p2.setuId(u_id);
//			Propertr_user p3=new Propertr_user();
//			p3.setpId(3);
//			p3.setCount(0);
//			p3.setuId(u_id);
//			propertr_userService.insertSelective(p1);
//			propertr_userService.insertSelective(p2);
//			propertr_userService.insertSelective(p3);
//			result="N";	
			
			}
		
		return result;
	}
}